<?php

namespace App\Http\Controllers\Home;

use App\Model\Category;
use App\Model\FollowUser;
use App\Models\Archive\Archive;
use App\Models\Archive\ArchiveType;
use App\Models\Archive\Gallery;
use App\Models\Tag\Tag;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class GalleryController extends Controller
{
    //
    const PAGE_COUNT = 20;
    public function index(Request $request, $cate = null)
    {
        $galleries = Archive
            ::select([
                'archives.id', 'title', 'cover','mode',
                \DB::raw('count(archive_id) as visit_count'),
                'abstract', 'u.nickname',
                \DB::raw('IF(qyg_u.head_img = "", "'.asset('img/100100.png').'", qyg_u.head_img) as head_img')
            ])
            ->ofPattern('review')
            ->where('is_del',0)
            ->where('archive_type_id', $this->getType())
            ->leftJoin('archive_visits', 'archives.id', 'archive_id')
            ->leftJoin('users as u', 'archives.user_id', 'u.id')
            ->groupBy('archives.id');

        if ($cate) $galleries->where('category_id', $cate);

        $galleries = $galleries->paginate(self::PAGE_COUNT);

        if ($request->ajax()) {
            $agent = $_SERVER['HTTP_USER_AGENT'];
            if(strpos($agent,"NetFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($agent,"UCWEB") || strpos($agent,"Android") || strpos($agent,"Windows CE") || strpos($agent,"SymbianOS")) {
                $galleries->map(function ($g) {
                    $g->tags = $g->tags()->get()->map(function ($t) {
                        $t->url = $t->url;
                        return $t;
                    });
                    $g->images = $g->images()->take(3)->get();
                    return $g;
                });
                return $galleries;
            } else {
                return response()->json($galleries);
            }
        } else {
            $cate_id = Category::where('cate_name','图库')->value('cate_id');
            $cate_info = Category::where('cate_id',$cate_id)->first();
            $category = Category::where('cate_pid',$cate_id)->where('is_on',0)->orderBy('cate_weight','asc')->orderBy('cate_id','desc')->get();

            $agent = $_SERVER['HTTP_USER_AGENT'];
            if(strpos($agent,"NetFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($agent,"UCWEB") || strpos($agent,"Android") || strpos($agent,"Windows CE") || strpos($agent,"SymbianOS")) {
                $galleries->map(function ($g) {
                    $g->tags = $g->tags()->get()->map(function ($t) {$t->url = $t->url; return $t;});
                    $g->images = $g->images()->take(3)->get();
                    return $g;
                });

                return view('mobile.gallery.gallery_list',compact('category', 'galleries','cate_info'));
            }else{
                return view('home.gallery.gallery_list',compact('category', 'galleries','cate_info'));
            }


        }
    }

    public function show(Request $request, Archive $archive)
    {
        if (!$archive->hasPattern('review')) return response('没有通过审核', 404);

        $res = $archive->toArray();
        $res['images'] = $archive->detail->images()->get()->toArray();
        $res['detail'] = $archive->detail->toArray();
        $res['author'] = $archive->user->nickname;
        $res['updated_at'] = worldTime(strtotime($archive->updated_at));
        $res['prev'] = Archive::where('archive_type_id', 2)->where('is_del',0)->ofPattern('review')->where('id', '<', $archive->id)->value('id');
        $res['next'] = Archive::where('archive_type_id', 2)->where('is_del',0)->ofPattern('review')->where('id', '>', $archive->id)->value('id');

        return response()->json($res);
    }

    private function getType()
    {
        return ArchiveType::where('name', 'gallery')->value('id');
    }
}
